﻿--[[
   @brief    ui logic for about wegame login first protocal 
   @author   planezhang
   @version  1.0
   @date     2020-07-16
--]]
login_first_protocal_view =
{    
    pDialog = nil;
    dwRootId = 0;
    protocal_version = 0;
    ierd_tgp.tpf_ui.Require("TPFLocalization.lua"); 
    check_box_flash = nil;
    check_box_flash_id = nil;
    check_box = nil;
    flash_img_index = 0;
    protocal_agree_text_box_id1 = 0;
    is_agreed = 0;
    init = function()
        ierd_tgp.tpf_ui.Log("login_first_protocal_view init.");
        login_first_protocal_view.pDialog = nil;
        login_first_protocal_view.dwRootId = 0;
        
        local tpfui = base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");
        if tpfui ~= nil then
            login_first_protocal_view.is_english_ver = tpfui:Is_English_ver();
        end;

    end;
    
    show = function(parent_id, version)
        ierd_tgp.tpf_ui.Log("login_first_protocal_view show,version:" .. tostring(version));
        login_first_protocal_view.protocal_version = version;
        if login_first_protocal_view.pDialog ~= nil then
            login_first_protocal_view.pDialog:Restore();
            login_first_protocal_view.pDialog:Show(TRUE);
            login_first_protocal_view.pDialog:BringToTop();
            return;
        end;
        
        local main_uilib = ui_utility.uilib();
        if main_uilib == nil then
            ierd_tgp.tpf_ui.Log("login_first_protocal_view get main_uilib failure!");
            return;
        end;
        
        --login_first_protocal_view.dwRootId = layout_mgr.restore_layout_ex(parent_id, "login\\first_login_protocal.tpf");
        login_first_protocal_view.dwRootId = layout_mgr.restore_layout("login\\first_login_protocal.tpf");
        login_first_protocal_view.protocal_agree_text_box_id1 = ui_utility.get_ctrl_id_by_path(
                                            login_first_protocal_view.dwRootId, "ProtocalRichText1");
        login_first_protocal_view.check_box_flash = ui_utility.get_ctrl_ptr_by_path(
                                            login_first_protocal_view.dwRootId, "ProtocalAgreeFlash");
        login_first_protocal_view.check_box = ui_utility.get_ctrl_ptr_by_path(
                                            login_first_protocal_view.dwRootId, "ProtocalAgreeBtn");
        login_first_protocal_view.check_box_flash_id = ui_utility.get_ctrl_id_by_path(
                                            login_first_protocal_view.dwRootId, "ProtocalAgreeFlash");
        local cancel_btn_id =  ui_utility.get_ctrl_id_by_path(
                                            login_first_protocal_view.dwRootId, "Btn_cancel");
        local pControl = main_uilib:GetControlPtr(login_first_protocal_view.dwRootId);
        login_first_protocal_view.pDialog = tolua.cast(pControl, "Tenio::ITDialog");
        if login_first_protocal_view.pDialog ~= nil then
            local protocal_agree_text_box_ptr = ui_utility.get_ctrl_ptr_by_id(login_first_protocal_view.protocal_agree_text_box_id1);
            if protocal_agree_text_box_ptr ~= nil then
                local sTemp = LoadStr("ID_TPF_FIRST_LOGIN_PROTOCAL_CONTENT");
                local   sText = U8toCUIStr(sTemp);
                protocal_agree_text_box_ptr:PushBackHtmlString(sText);
                local scale_dpi = ui_utility.get_dpi_scale();
                ierd_tgp.tpf_ui.Log("login_first_protocal_view scale_dpi:"..tostring(scale_dpi));
                local text_rect = protocal_agree_text_box_ptr:GetRectEx(FALSE,TRUE);
                
                local warning_text =  ui_utility.get_ctrl_id_by_path(
                                            login_first_protocal_view.dwRootId, "Warning_Text");
                local warning_text_ptr = ui_utility.get_ctrl_ptr_by_id(warning_text);
                local warning_text_rect = nil;
                if warning_text_ptr ~= nil then
                    warning_text_rect = warning_text_ptr:GetRectEx(FALSE,TRUE);
                end;
                if scale_dpi < 1.25 then
                    if warning_text_rect~=nil then
                        warning_text_rect.top = warning_text_rect.top - 2;
                        warning_text_rect.bottom = warning_text_rect.bottom - 2;
                        warning_text_ptr:MoveEx(warning_text_rect,TRUE);
                    end;
                end;
                
                if scale_dpi == 1.25 then
                    text_rect.left = text_rect.left + 10;
                    text_rect.right = text_rect.right + 10;
                    text_rect.top = text_rect.top + 2;
                    text_rect.bottom = text_rect.bottom + 2;
                    protocal_agree_text_box_ptr:MoveEx(text_rect,TRUE);
                    if warning_text_rect~=nil then
                        warning_text_rect.left = warning_text_rect.left + 10;
                        warning_text_rect.right = warning_text_rect.right + 10;
                        warning_text_rect.top = warning_text_rect.top - 3;
                        warning_text_rect.bottom = warning_text_rect.bottom - 3;
                        warning_text_ptr:MoveEx(warning_text_rect,TRUE);
                    end;
                end;
                 
                if scale_dpi >= 1.44 and scale_dpi <2.0 then
                    text_rect.left = text_rect.left + 6;
                    text_rect.right = text_rect.right + 6;
                    text_rect.top = text_rect.top + 1;
                    text_rect.bottom = text_rect.bottom + 1;
                    protocal_agree_text_box_ptr:MoveEx(text_rect,TRUE);
                    
                    if warning_text_rect~=nil then
                        warning_text_rect.left = warning_text_rect.left + 5;
                        warning_text_rect.right = warning_text_rect.right + 5;
                        if scale_dpi >=1.44 and scale_dpi <1.679 then
                            warning_text_rect.top = warning_text_rect.top - 3;
                            warning_text_rect.bottom = warning_text_rect.bottom - 3;
                        elseif scale_dpi >=1.679 and scale_dpi < 2.0 then
                            warning_text_rect.top = warning_text_rect.top - 5;
                            warning_text_rect.bottom = warning_text_rect.bottom - 5;
                        end;
                        warning_text_ptr:MoveEx(warning_text_rect,TRUE);
                    end;
                    
                    
                end;
                
                
                if scale_dpi == 2.0 then
                    text_rect.top = text_rect.top + 1;
                    text_rect.bottom = text_rect.bottom + 1;
                    protocal_agree_text_box_ptr:MoveEx(text_rect,TRUE);
                    
                    if warning_text_rect~=nil then
                        warning_text_rect.top = warning_text_rect.top - 5;
                        warning_text_rect.bottom = warning_text_rect.bottom - 5;
                        warning_text_ptr:MoveEx(warning_text_rect,TRUE);
                    end;
                end;
            end;
            -- en
            if base_utility.get_current_language() == 2 then
                local cancel_button_text = LoadStr("ID_TPF_CANCEL");
                ui_utility.setproperty_by_id(cancel_btn_id, "Width", "48");
                ui_utility.setproperty_by_id(cancel_btn_id, "Caption", cancel_button_text);
            end;

            login_first_protocal_view.pDialog:Show(TRUE);
            login_first_protocal_view.pDialog:BringToTop();
        else
            ierd_tgp.tpf_ui.Log("create about dialog error!!!");
            return;
        end;        
    end;
    
    close = function(uilib, dwControlId)
        local warning_text =  ui_utility.get_ctrl_id_by_path(
                                            login_first_protocal_view.dwRootId, "Warning_Text");
            if warning_text ~=nil then
                ui_utility.setproperty_by_id(warning_text, "Visible", "false");
            end;
        if login_first_protocal_view.pDialog ~= nil then
            login_first_protocal_view.pDialog:Show(FALSE);
        end;
        local params = {agreed = 0, protocal_version = login_first_protocal_view.protocal_version};
        layout_mgr.delay_remove_ctrl(login_first_protocal_view.dwRootId);
        login_first_protocal_view.init();
        local agree_service = comm_center.call_service("Svr_LoginProtocalAgreed", params);
    end;

     on_hyperlink_click = function(uilib, dwControlId, hyberlink_rc, hyberlink_id, hyberlink_name, left, top, keystate, ctrl,is_shift,is_alt, hyberlink)
        local hyberlink_id_text = string.format("%d", hyberlink_id);
        TGPLog.LogI("first_login_protocal", "on_hyperlink_click:" ..hyberlink_id_text);
        --  hyberlink_id是在language.xml 处STR_LOGIN_OFFSITE_LOGIN设定的
        local open_url = "";
        if hyberlink_id == 1 then
            TGPLog.LogI("first_login_protocal", "open software protocal.");
            open_url = LoadStr("ID_TPF_FIRST_LOGIN_PROTOCAL_SOFTWARE_PROTOCAL");
        elseif hyberlink_id == 2 then
            TGPLog.LogI("first_login_protocal", "privacy protocal.");
            open_url = LoadStr("ID_TPF_FIRST_LOGIN_PROTOCAL_PRIVACY_PROTOCAL");
        elseif hyberlink_id == 3 then
            TGPLog.LogI("first_login_protocal", "children protocal.");
            open_url = LoadStr("ID_TPF_FIRST_LOGIN_PROTOCAL_CHILDREN_PROTOCAL");
        end;
        if open_url~= "" then
                --extra_info = {need_broadcast = true, from = "open_detail_page_or_start_download"};
                local param = { 
                    type = "open_web", 
                    browser = "sys", 
                    use_ptlogin = "1", 
                    url = open_url
                };
            --web_utility.open_link(param);
            web_utility.open_link(
                '{"type":"open_web", "browser":"sys", "url":"'..open_url..'", "use_ptlogin":"0"}');
        end;
    end;
    
    onok = function(uilib, dwControlId)
        if login_first_protocal_view.is_agreed == 0 then
            local warning_text =  ui_utility.get_ctrl_id_by_path(
                                            login_first_protocal_view.dwRootId, "Warning_Text");
            --[[if warning_text ~=nil then
                ui_utility.setproperty_by_id(warning_text, "Visible", "true");
            end;]]--
                                            
                                            
            login_first_protocal_view.StartAniTmr();
            return;
        end;
        if login_first_protocal_view.pDialog ~= nil then
            ierd_tgp.tpf_ui.Log("login_first_protocal_view hide dialog");
            login_first_protocal_view.pDialog:Show(FALSE);
        end;
        
        local params = {agreed = 1, protocal_version = login_first_protocal_view.protocal_version};
        layout_mgr.delay_remove_ctrl(login_first_protocal_view.dwRootId);
        login_first_protocal_view.init();
        local agree_service = comm_center.call_service("Svr_LoginProtocalAgreed", params);
    end;
    
    on_protocal_agreed = function(uilib, ctrl_id)
        login_first_protocal_view.is_agreed = 1;
    end;
    
    on_protocal_disagreed = function(uilib, ctrl_id)
        login_first_protocal_view.is_agreed = 0;
    end;

    close_view = function ()
        if login_first_protocal_view.pDialog ~= nil then            login_first_protocal_view.pDialog:Show(FALSE);
            layout_mgr.delay_remove_ctrl(login_first_protocal_view.dwRootId);
            login_first_protocal_view.init();
        end;
    end;
    
    StartAniTmr = function()
        if login_first_protocal_view.check_box == nil or login_first_protocal_view.check_box_flash == nil then
            return;
        end;
        login_first_protocal_view.flash_img_index = 0;
        local check_box_rc = login_first_protocal_view.check_box:GetRectEx(FALSE,FALSE);
        local check_box_img_rc = login_first_protocal_view.check_box:GetCheckBoxRect();
        local check_box_flash_rc = check_box_rc;
        check_box_flash_rc.left = check_box_flash_rc.left + check_box_img_rc.left;
        check_box_flash_rc.top = check_box_flash_rc.top + check_box_img_rc.top;
        check_box_flash_rc.right = check_box_flash_rc.left + (check_box_img_rc.right - check_box_img_rc.left);
        check_box_flash_rc.bottom = check_box_flash_rc.top + (check_box_img_rc.bottom - check_box_img_rc.top);
        
        login_first_protocal_view.check_box_flash:MoveEx(check_box_flash_rc);
        
        ui_utility.setproperty_by_id(login_first_protocal_view.check_box_flash_id, "Visible", "true");
        local pTPFUI = base_utility.get_component_adapter("ITpf_ui",
                            "ierd_tgp.tpf_ui.GetTPFUI();");

        if nil ~= pTPFUI then
            pTPFUI:AddInterval("login_first_protocal_view.OnBtnAniTmr", 1, 0);
            
        else
            TGPLog.LogE("login_first_protocal_view", "Add interval, TPFUI is null.");
        end;
    end;

    StopAniTmr = function()
        local pTPFUI = base_utility.get_component_adapter("ITpf_ui",
                            "ierd_tgp.tpf_ui.GetTPFUI();");

        if nil ~= pTPFUI then
            pTPFUI:RemoveInterval("login_first_protocal_view.OnBtnAniTmr", 0);
        else
            TGPLog.LogE("login_first_protocal_view", "Add interval, TPFUI is null.");
        end;
        login_first_protocal_view.flash_img_index = 0;
        ui_utility.setproperty_by_id(login_first_protocal_view.check_box_flash_id, "Visible", "false");
    end;

    OnBtnAniTmr = function()
        if login_first_protocal_view.flash_img_index >= 7 then
            login_first_protocal_view.StopAniTmr();
            return;
        end;
        local image_index = "0" .. tostring(login_first_protocal_view.flash_img_index + 1) .. ".png";
        local background_img = "ONEIMAGE:client_login\\protocal\\check_box_flash\\" .. image_index .."?0,0,12,12;Stretch;null;";
        if login_first_protocal_view.check_box ~= nil and login_first_protocal_view.check_box_flash ~= nil  then
            ui_utility.setproperty_by_id(login_first_protocal_view.check_box_flash_id, "Background", background_img);
        end;
        login_first_protocal_view.flash_img_index = login_first_protocal_view.flash_img_index + 2;
    end;
};
login_first_protocal_view.init();